namespace :assets do
  
  desc 'Compile .coffee and .sass files in /public'
  task :precompile do
    settings.assets_path.each do |path|
      copy_directory("./#{path}", "./public/#{path}") unless path == 'public'
    end
    Dir.glob("./public/**/*.coffee").each do |asset|
      File.open(asset.gsub('.coffee', '.js'), 'w') do |f|
        f.write(JSMin.minify(CoffeeScript.compile(File.read(asset), bare: Tilt::CoffeeScriptTemplate.default_bare)).strip)
      end
    end
    Dir.glob("./public/**/*.scss").each do |asset|
      File.open(asset.gsub('.scss', '.css'), 'w') do |f|
        f.write(Sass::Engine.for_file(asset, { style: :compressed }).render)
      end
    end
    settings.assets_path.each do |path|
      unless path == 'public'
        Dir.glob("./public/#{path}/**/*.{coffee,scss}").each do |asset|
          File.delete(asset)
        end
      end
    end
  end
  
  desc 'Remove compiled assets'
  task :decompile do
    settings.assets_path.each do |path|
      FileUtils.rm_rf("./public/#{path}") unless path == 'public'
    end
    Dir.glob("./public/**/*.coffee").each do |asset|
      File.delete(asset.gsub('.coffee', '.js')) if File.exists?(asset.gsub('.coffee', '.js'))
    end
    Dir.glob("./public/**/*.scss").each do |asset|
      File.delete(asset.gsub('.scss', '.css')) if File.exists?(asset.gsub('.scss', '.css'))
    end
  end
  
end
